package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.*
import com.af.community.smart.model.ResultData
import com.af.community.smart.utils.DialogHelper.showItemDialog
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.utils.textString
import com.af.community.smart.viewmodel.HouseViewModel
import kotlinx.android.synthetic.main.activity_house.*
import kotlinx.android.synthetic.main.layout_title_search_hint.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HouseActivity : BaseActivity() {

    private val areaList = ArrayList<ResultData>()
    private val buildList = ArrayList<ResultData>()
    private var mVillageId = ""
    private var mVillageName = ""
    private var mBuildId = ""
    private var mBuildName = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_house)
        setToolbarVisibility(false)
        initTitle()
    }

    override fun getViewModel() = getViewModel<HouseViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        search_right.text = "添加房屋"
        house_district.setLeftString(getString("countryName"))
        house_country.setLeftString(getString("streetName"))

        house_area.oneClick {
            if (areaList.isEmpty()) {
                getViewModel().getGridVillage(getString("gridId"))
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByFinally(
                        event = getViewModel(),
                        onSuccess = { result ->
                            areaList.addItems(result)
                            if (areaList.isNotEmpty()) {
                                val items = ArrayList<String>()
                                areaList.mapTo(items) { it.title }
                                showItemDialog(
                                    "请选择村/小区",
                                    items.indexOf(mVillageName),
                                    items
                                ) { pos, hint ->
                                    mVillageName = hint
                                    mVillageId = areaList[pos].type
                                    house_area.setLeftString(hint)
                                    mBuildId = ""
                                    mBuildName = ""
                                    house_build.setLeftString("")
                                }
                            }
                        }
                    )
            } else {
                val items = ArrayList<String>()
                areaList.mapTo(items) { it.title }
                showItemDialog(
                    "请选择村/小区",
                    items.indexOf(mVillageName),
                    items
                ) { pos, hint ->
                    mVillageName = hint
                    mVillageId = areaList[pos].type
                    house_area.setLeftString(hint)
                    mBuildId = ""
                    mBuildName = ""
                    house_build.setLeftString("")
                }
            }
        }
        house_build.oneClick {
            if (mVillageId.isEmpty()) {
                toast("请选择所属村/小区")
                return@oneClick
            }

            getViewModel().getGridBuilding(
                getString("gridId"),
                mVillageId
            )
                .bindLifeCycle(getLifecycleOwner())
                .subscribeByFinally(
                    event = getViewModel(),
                    onSuccess = { result ->
                        buildList.clear()
                        buildList.addItems(result)
                        if (buildList.isNotEmpty()) {
                            val items = ArrayList<String>()
                            buildList.mapTo(items) { it.title }
                            showItemDialog(
                                "请选择楼栋",
                                items.indexOf(mBuildName),
                                items
                            ) { pos, hint ->
                                mBuildName = hint
                                mBuildId = buildList[pos].type
                                house_build.setLeftString(hint)
                            }
                        }
                    }
                )
        }
        search_hint.oneClick { startActivity<HouseListActivity>() }
        search_right.oneClick { startActivity<HouseSelectActivity>() }
        bt_look.oneClick {
            if (house_unit.text.isNotBlank()
                || house_floor.text.isNotBlank()
                || house_num.text.isNotBlank()
            ) {
                if (mVillageId.isEmpty()) {
                    toast("请选择所属村/小区")
                    return@oneClick
                }

                if (mBuildId.isEmpty()) {
                    toast("请选择所属楼栋")
                    return@oneClick
                }

                when {
                    house_unit.text.isBlank() && house_floor.text.isNotBlank() -> {
                        toast("请输入所属单元")
                        return@oneClick
                    }
                    house_floor.text.isBlank() && house_num.text.isNotBlank() -> {
                        toast("请输入所属楼层")
                        return@oneClick
                    }
                }
            }
            startActivity<HouseListActivity>(
                "villageId" to mVillageId,
                "buildId" to mBuildId,
                "cellName" to house_unit.textString(),
                "floorName" to house_floor.textString(),
                "roomName" to house_num.textString()
            )

            getViewModel()
                .startDelay()
                .bindLifeCycle(getLifecycleOwner())
                .subscribe { resetInput() }
        }
        bt_reset.oneClick { resetInput() }
    }

    private fun resetInput() {
        mVillageId = ""
        mVillageName = ""
        mBuildId = ""
        mBuildName = ""
        house_area.setLeftString("")
        house_build.setLeftString("")
        house_unit.clear()
        house_floor.clear()
        house_num.clear()
    }

}
